package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentBudget;
import com.thinkingstar.iads.projectcenter.entity.PcProjectInfo;

/**
 * @ClassName: DaoEquipmentBudget
 * @Description:数据中心_
 * @date 2018年6月19日
 */
@IocBean(args = {"refer:dao"})
public class DaoEquipmentBudget extends EntityRepositoryServiceImpl<DcEquipmentBudget>{

	public DaoEquipmentBudget(Dao dao) {
		super(dao);
	}
	
	//根据设计阶段,设计专业,设计内容查询设备概算
	public DcEquipmentBudget findEquipmentBudgetByPhaseIdAndMajorIdAndContentId(PcProjectInfo pcProjectInfo){
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false)
		.and("project_design_phase_id", "=", pcProjectInfo.getProject_design_phase_id())
		.and("project_design_content_id", "=", pcProjectInfo.getProject_design_content_id())
		.and("project_design_major_id", "=", pcProjectInfo.getProject_design_major_id());
		List<DcEquipmentBudget> result_list = query(criteria);
		if(!result_list.isEmpty()){
			return result_list.get(0);
		}
		return null;
	}
	
	//分页查询设备概算表
	public QueryResult findEquipmentBudgetList(NutMap param){
		List <NutMap> page_list = null;
		Pager pager = null;
		StringBuilder find_sql = new StringBuilder(" select dpeb.id , "
				+ " dpeb.equipment_budget_table_code , dpeb.equipment_budget_table_name ,"
				+ " dpdp.project_design_phase_name , dpdc.project_design_content_name ,"
				+ " dpdm.project_design_major_name "
				+ " from dc_project_equipment_budget dpeb "
				+ " left join dc_project_design_phase dpdp on dpeb.project_design_phase_id = dpdp.id and dpdp.del_flag = '0' "
				+ " left join dc_project_design_content dpdc on dpeb.project_design_content_id = dpdc.id and dpdc.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on dpeb.project_design_major_id = dpdm.id and dpdm.del_flag = '0' "
				+ " where dpeb.del_flag = '0' ");
		if(param.get("budget_name") != null){
			find_sql.append(" and name like @budget_name");
		}
		Sql sql = Sqls.create(find_sql.toString());
		sql.params().set("budget_name", "%"+param.getString("budget_name")+"%");
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"), param.getInt("page_size"));
			pager.setRecordCount(findEquipmentBudgetNum(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<NutMap> list = new ArrayList<>();
				while (rs.next()){
					NutMap map = new NutMap();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed));
					}
					list.add(map);
				}
				return list;
			}
		});
		dao().execute(sql);
		page_list = sql.getList(NutMap.class);
		return new QueryResult(page_list,pager);
	}
	
	//查询设备概算表数量
	public int findEquipmentBudgetNum(NutMap param){
		StringBuilder find_sql = new StringBuilder(" select count(dpeb.id) record_num "
				+ " from dc_project_equipment_budget dpeb "
				+ " left join dc_project_design_phase dpdp on dpeb.project_design_phase_id = dpdp.id and dpdp.del_flag = '0' "
				+ " left join dc_project_design_content dpdc on dpeb.project_design_content_id = dpdc.id and dpdc.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on dpeb.project_design_major_id = dpdm.id and dpdm.del_flag = '0' "
				+ " where dpeb.del_flag = '0' ");
		if(param.get("budget_name") != null){
			find_sql.append(" and name like @budget_name");
		}
		Sql sql = Sqls.create(find_sql.toString());
		sql.params().set("budget_name", "%"+param.getString("budget_name")+"%");
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				int record_num = 0;
				while (rs.next()) {
					record_num = rs.getInt("record_num");
				}
				return record_num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	
	

}
